//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.IO.NetIO;

import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SocketChannel;

public class StreamIO extends NetIO {
    protected void PollIn() {
        try {
            ByteBuffer buffer = this.session.ibuffer.getByteBuffer(this.session.ibuffer.size(), this.session.ibuffer.capacity() - this.session.ibuffer.size());
            if (((SocketChannel)this.channel).read(buffer) > 0) {
                this.session.ibuffer.resize(buffer.position());
                return;
            }
        } catch (Exception var2) {
        }

        this.session.obuffer.clear();
        this.session.closing = true;
    }

    protected void PollOut() {
        try {
            ByteBuffer buffer = this.session.obuffer.getByteBuffer(0, this.session.obuffer.size());
            if (((SocketChannel)this.channel).write(buffer) > 0) {
                this.session.obuffer.erase(0, buffer.position());
                return;
            }
        } catch (Exception var2) {
        }

        this.session.obuffer.clear();
        this.session.closing = true;
    }

    protected int UpdateEvent() {
        int event = 0;
        synchronized(this.session) {
            if (this.session.ibuffer.size() > 0) {
                this.session.OnRecv();
            }

            if (!this.session.closing) {
                this.session.OnSend();
            }

            if (this.session.obuffer.size() > 0) {
                event = 4;
            }

            if (this.session.closing) {
                try {
                    this.channel.close();
                } catch (Exception var4) {
                }

                return -1;
            } else {
                if (this.session.ibuffer.size() < this.session.ibuffer.capacity()) {
                    event |= 1;
                }

                return event;
            }
        }
    }

    public StreamIO(SelectableChannel sc, NetSession s) {
        super(sc, s);
        s.OnOpen();
    }
}
